<template>
  <input
    type="file"
    ref="videoRef"
    style="display: none"
    accept="video/*"
    @change="videoChange"
  />
  <i title="视频" class="iconfont icon-shipin" @click="videoClick" />
</template>

<script setup>
import { getCurrentInstance, ref } from "vue";
import { useUserInfoStore } from "@/stores/UserInfoStore";

const { proxy } = getCurrentInstance();
const userInfoStore = useUserInfoStore();

const props = defineProps({
  contactId: {
    type: String,
  },
});

const videoRef = ref();
const videoClick = async () => {
  videoRef.value.click();
};
const videoChange = async (e) => {
  const prefix = props.contactId.substring(0, 1);
  if (e.target.files.length > 0) {
    const file = e.target.files[0];
    if (file.size > 150 * 1024 * 1024) {
      proxy.$message.error("视频大于150M");
      return;
    }
    const formData = new FormData();
    formData.append("file", file);
    const resUpload = await proxy.Api.minioUpload(formData);
    if (resUpload.code !== 200) {
      proxy.$message.error(resUpload.message);
      return;
    } else {
      if (prefix === "U") {
        const res = await proxy.Api.sendPrivateMessage({
          deduplicationId: proxy.UniqueId.generateUniqueUserId(),
          sendUserId: userInfoStore.getInfo().userId,
          contactId: props.contactId,
          messageType: "video",
          messageContent: resUpload.data,
          fileSize: file.size,
          fileName: file.name,
        });
        if (res.code == 114) {
          proxy.Confirm({
            message: res.message,
            okfun: () => {
              addContact(props.currentChatUser.contactId, res.code);
            },
            okText: "重新申请",
          });
        } else if (res.code == 200) {
          proxy.$message.success("视频已发送");
        }
        return;
      } else if (prefix === "G") {
        const res = await proxy.Api.sendClusterMessage({
          deduplicationId: proxy.UniqueId.generateUniqueClusterId(),
          sendUserId: userInfoStore.getInfo().userId,
          contactId: props.contactId,
          messageType: "video",
          messageContent: resUpload.data,
          fileSize: file.size,
          fileName: file.name,
        });
        if (res.code === 119) {
          proxy.Confirm({
            message: res.message,
            okfun: () => {
              addContact(props.currentChatUser.contactId, res.code);
            },
            okText: "重新申请",
          });
        } else if (res.code === 200) {
          proxy.$message.success("视频已发送");
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.iconfont {
  margin-left: 10px;
  cursor: pointer;
}
</style>
